﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.CampaignSystem.Settlements.Workshops.WorkshopType
// Assembly: TaleWorlds.CampaignSystem, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E85F8C15-4DF6-4E9C-A58A-29177E40D07A
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\bin\Win64_Shipping_Client\TaleWorlds.CampaignSystem.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using TaleWorlds.Core;
using TaleWorlds.Library;
using TaleWorlds.Localization;
using TaleWorlds.ObjectSystem;

#nullable disable
namespace TaleWorlds.CampaignSystem.Settlements.Workshops
{
  public sealed class WorkshopType : MBObjectBase
  {
    private MBList<WorkshopType.Production> _productions;

    internal static void AutoGeneratedStaticCollectObjectsWorkshopType(
      object o,
      List<object> collectedObjects)
    {
      ((MBObjectBase) o).AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    protected override void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
    {
      base.AutoGeneratedInstanceCollectObjects(collectedObjects);
    }

    public int EquipmentCost { get; private set; }

    public int Frequency { get; private set; }

    public TextObject Name { get; private set; }

    public TextObject JobName { get; private set; }

    public bool IsHidden { get; private set; }

    public string SignMeshName { get; private set; }

    public string PropMeshName1 { get; private set; }

    public string PropMeshName2 { get; private set; }

    public List<string> PropMeshName3List { get; private set; }

    public string PropMeshName4 { get; private set; }

    public string PropMeshName5 { get; private set; }

    public string PropMeshName6 { get; private set; }

    public TextObject Description { get; private set; }

    public MBReadOnlyList<WorkshopType.Production> Productions
    {
      get => (MBReadOnlyList<WorkshopType.Production>) this._productions;
    }

    public override string ToString() => this.Name.ToString();

    public override void Initialize()
    {
      base.Initialize();
      this._productions = new MBList<WorkshopType.Production>(0);
    }

    public override void Deserialize(MBObjectManager objectManager, XmlNode node)
    {
      base.Deserialize(objectManager, node);
      this.Name = new TextObject(node.Attributes["name"].Value);
      this.JobName = new TextObject(node.Attributes["jobname"].Value);
      this.Description = new TextObject(node.Attributes["description"].Value);
      this.EquipmentCost = int.Parse(node.Attributes["equipment_cost"].Value);
      this.Frequency = node.Attributes["frequency"] != null ? int.Parse(node.Attributes["frequency"].Value) : 1;
      this.IsHidden = node.Attributes["isHidden"] != null && bool.Parse(node.Attributes["isHidden"].Value);
      this._productions = new MBList<WorkshopType.Production>();
      foreach (XmlNode childNode in node.ChildNodes)
      {
        if (childNode.Name == "Production")
        {
          objectManager.CreateObjectFromXmlNode(childNode);
          WorkshopType.Production production = new WorkshopType.Production(childNode.Attributes["conversion_speed"] != null ? float.Parse(childNode.Attributes["conversion_speed"].Value) : 0.0f);
          foreach (XmlNode xmlNode1 in childNode)
          {
            if (xmlNode1.Name == "Inputs")
            {
              foreach (XmlNode xmlNode2 in xmlNode1)
              {
                if (xmlNode2.Attributes != null && xmlNode2.Attributes.Count > 0)
                {
                  string objectName = xmlNode2.Attributes[0].Value;
                  if (!string.IsNullOrEmpty(objectName))
                  {
                    ItemCategory itemCategory = objectManager.GetObject<ItemCategory>(objectName);
                    int count = xmlNode2.Attributes["input_count"] != null ? int.Parse(xmlNode2.Attributes["input_count"].Value) : 1;
                    if (itemCategory != null)
                      production.AddInput(itemCategory, count);
                    else
                      Debug.Print("While reading Workshop Node: " + node.ToString() + " Unable to Find Item Category:" + objectName);
                  }
                  else
                    Debug.FailedAssert("false", "C:\\Develop\\MB3\\Source\\Bannerlord\\TaleWorlds.CampaignSystem\\Settlements\\Workshops\\WorkshopType.cs", nameof (Deserialize), 165);
                }
              }
            }
            if (xmlNode1.Name == "Outputs")
            {
              foreach (XmlNode node1 in xmlNode1)
              {
                if (node1.Attributes.Count > 0)
                {
                  objectManager.CreateObjectFromXmlNode(node1);
                  ItemCategory outputCategory = objectManager.ReadObjectReferenceFromXml<ItemCategory>("output", node1);
                  int outputCount = node1.Attributes["output_count"] != null ? int.Parse(node1.Attributes["output_count"].Value) : 1;
                  production.AddOutput(outputCategory, outputCount);
                }
              }
            }
          }
          this._productions.Add(production);
        }
        else if (childNode.Name == "Meshes")
        {
          this.SignMeshName = XmlHelper.ReadString(childNode, "sign_mesh_name");
          this.PropMeshName1 = XmlHelper.ReadString(childNode, "shop_prop_mesh_name_1");
          this.PropMeshName2 = XmlHelper.ReadString(childNode, "shop_prop_mesh_name_2");
          this.PropMeshName3List = new List<string>();
          for (int index = 1; index < 4; ++index)
          {
            string name = "shop_prop_mesh_name_3_" + (object) index;
            if (childNode.Attributes[name] != null)
              this.PropMeshName3List.Add(childNode.Attributes[name].Value);
          }
          this.PropMeshName4 = XmlHelper.ReadString(childNode, "shop_prop_mesh_name_4");
          this.PropMeshName5 = XmlHelper.ReadString(childNode, "shop_prop_mesh_name_5");
          this.PropMeshName6 = XmlHelper.ReadString(childNode, "shop_prop_mesh_name_6");
        }
      }
      this._productions.Capacity = this._productions.Count;
    }

    public static WorkshopType Find(string idString)
    {
      return MBObjectManager.Instance.GetObject<WorkshopType>(idString);
    }

    public static WorkshopType FindFirst(Func<WorkshopType, bool> predicate)
    {
      return WorkshopType.All.FirstOrDefault<WorkshopType>(predicate);
    }

    public static MBReadOnlyList<WorkshopType> All => Campaign.Current.Workshops;

    public struct Production
    {
      private MBList<(ItemCategory, int)> _inputs;
      private MBList<(ItemCategory, int)> _outputs;
      private float _conversionSpeed;

      public Production(float conversionSpeed)
      {
        this._inputs = new MBList<(ItemCategory, int)>();
        this._outputs = new MBList<(ItemCategory, int)>();
        this._conversionSpeed = conversionSpeed;
      }

      public void AddInput(ItemCategory item, int count = 1) => this._inputs.Add((item, count));

      public void AddOutput(ItemCategory outputCategory, int outputCount)
      {
        this._outputs.Add((outputCategory, outputCount));
      }

      public MBReadOnlyList<(ItemCategory, int)> Inputs
      {
        get => (MBReadOnlyList<(ItemCategory, int)>) this._inputs;
      }

      public MBReadOnlyList<(ItemCategory, int)> Outputs
      {
        get => (MBReadOnlyList<(ItemCategory, int)>) this._outputs;
      }

      public float ConversionSpeed => this._conversionSpeed;

      public override string ToString()
      {
        string str1 = "in: ";
        foreach ((ItemCategory, int) input in (List<(ItemCategory, int)>) this._inputs)
          str1 = str1 + (object) input.Item1 + ": ";
        string str2 = str1 + "out: ";
        foreach ((ItemCategory, int) output in (List<(ItemCategory, int)>) this._outputs)
          str2 = str2 + (object) output.Item1 + ": ";
        return str2;
      }
    }
  }
}
